const net=require('net')
const tcp_server=net.createServer()


module.exports=Server={
    init(ebus){
        this.sockets=[]
        tcp_server.listen(3000,function(){
            console.log('tcp_server listening 3000')
        })
        tcp_server.on('connection',socket=>{
            console.log('连接了',socket.address())
            this.sockets.push(socket)
            socket.on('close',()=>{
                let index=this.sockets.indexOf(socket)
                if(index>=0){
                    this.sockets.splice(index,1)
                }
            })
        })

        ebus.on('cmd',(data) => {
            this.sockets.forEach(socket=>{
                socket.write(JSON.stringify(data.message))
            })
            
        })
    }
}


